/*
ESP-NOW 数据桥接

1.不管哪个设备发送的数据都会转发一次
2.根据消息id进行判断是否需要转发和处理本次消息

*/
#include <WiFi.h>
#include <esp_now.h>

#include "./EspNowSend.h"
#include "./EspNowRecv.h"
#include "./Config.h"

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);


  // 初始化引脚为输出模式
  pinMode(led1_pin, OUTPUT);


  // 初始化 WiFi 为 Station 模式
  WiFi.mode(WIFI_STA);

  // 初始化 ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }


  Serial.println("ESP-NOW Example - Broadcast Master");
  Serial.println("Wi-Fi parameters:");
  Serial.println("Mode: STA");
  Serial.println("MAC Address: " + WiFi.macAddress());
  uint8_t mac[6];
  WiFi.macAddress(mac);
  sprintf(thisMacAddressStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);



  //初始化发送
  InitEspNowSend();
  //初始化接收
  InitEspNowRecv();
}
//获取随机数
int randomNumber = random(10, 21);
//定时中断 100ms 触发一次
void timer_100ms() {


  //随机发送一条消息
  randomNumber--;
  if (randomNumber <= 0) {
    randomNumber = random(100, 210);
    // 准备要发送的数据
    sprintf(myData.data, "Hello ESP-NOW From MAC:%s", thisMacAddressStr);
    sprintf(myData.id, "%d", random(1, 9999999));
    sprintf(myData.sendMac, "%s", thisMacAddressStr);
    //发送随机消息
    EspNowSend(broadcastAddress, (uint8_t *)&myData, sizeof(myData));
  }


  //100ms判断一次是否有需要发送的数据
  LoopEspNowSend();
}

void loop() {

  delay(100);
  timer_100ms();
}
